<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
//header('Content-type: text/html');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');


$code = $_REQUEST["code"];
$callback = !empty($_REQUEST['callback']) ? $_REQUEST['callback'] : 'fbAuthCallback';
$accessToken = !empty($_REQUEST['accessToken']) ? $_REQUEST['accessToken'] : 'xxx';
$itemId = !empty($_REQUEST['itemId']) ? $_REQUEST['itemId'] : 'xxx';
$method = !empty($_REQUEST['method']) ? $_REQUEST['method'] : '';
$command = !empty($_REQUEST['command']) ? $_REQUEST['command'] : '';

$message = !empty($_REQUEST['message']) ? $_REQUEST['message'] : '';
$picture = !empty($_REQUEST['picture']) ? $_REQUEST['picture'] : '';
$link = !empty($_REQUEST['link']) ? $_REQUEST['link'] : '';
$name = !empty($_REQUEST['name']) ? $_REQUEST['name'] : '';
$caption = !empty($_REQUEST['caption']) ? $_REQUEST['caption'] : '';
$description = !empty($_REQUEST['description']) ? $_REQUEST['description'] : '';
$source = !empty($_REQUEST['source']) ? $_REQUEST['source'] : '';

$host = $_SERVER['HTTP_HOST'];
$uri = trim(dirname($_SERVER['PHP_SELF']), '/\\');
$myUrl = "http://$host/$uri/fb.post.php";

if ($command)
	$token_url = "https://graph.facebook.com/$itemId/$command";
else
	$token_url = "https://graph.facebook.com/$itemId";

$dataAr = array(
	'access_token' => $accessToken,
);

if ($message) $dataAr['message'] = $message;
if ($picture) $dataAr['picture'] = $picture;
if ($link) $dataAr['line'] = $link;
if ($name) $dataAr['name'] = $name;
if ($caption) $dataAr['caption'] = $caption;
if ($description) $dataAr['description'] = $description;
if ($source) $dataAr['source'] = $source;

if ($method) $dataAr['method'] = $method;

$data = http_build_query($dataAr);

function do_post_request($url, $data, $optional_headers = null) {
	$params = array('http' => array(
		'method' => 'POST',
		'content' => $data,
		));
	if ($optional_headers !== null) {
		$params['http']['header'] = $optional_headers;
	}
	$ctx = stream_context_create($params);
	$fp = @fopen($url, 'rb', TRUE, $ctx);
	if (!$fp) {
		header('Content-type: text/plain');
		echo "url=$url\n";
		echo print_r($data, TRUE);
		echo print_r($ctx, TRUE);
		echo print_r($php_errormsg, TRUE);
		//echo "error message=$php_errormsg\N";
		//throw new Exception("Problem with $url, $php_errormsg");
		exit(0);
	}
	$response = @stream_get_contents($fp);
	if ($response === false) {
		throw new Exception("Problem reading data from $url, $php_errormsg");
	}
	return $response;
}

$reply = do_post_request($token_url, $data);

$ot = '';
$ot .= "$callback({\n";
$ot .= "\"result\":" . json_encode($result) . ",\n";
$ot .= "\"return\":" . json_encode($reply) . ",\n";
//$ot .= "\"user\":" . ($json_user ? $json_user : '""') . ",\n";
$ot .= "\"access_token\":" . json_encode($accessToken) . ",\n";
$ot .= "\"end\":true\n";
$ot .= "});";
echo $ot;
?>